<?php

require_once("lib/exception/internal_error_exception.php");
require_once("lib/exception/instance_not_found_exception.php");

require_once("lib/model/action.php");

require_once("model/article/dao/article_dao_factory.php");
require_once("model/article_article_category/dao/article_article_category_dao_factory.php");
require_once("model/article_category/dao/article_category_dao_factory.php");
require_once("model/article_description/dao/article_description_dao_factory.php");
require_once("model/article_link/dao/article_link_dao_factory.php");
require_once("model/article_image/dao/article_image_dao_factory.php");
require_once("model/article_video/dao/article_video_dao_factory.php");

require_once("model/articlefacade/to/basic_category_to.php");
require_once("model/articlefacade/to/basic_article_to.php");

class Get_basic_article_action implements Action {

	private $number;
	
	public function __construct($number) {
		$this->number = $number;
	}
	
	public function do_execute() {
		try {
			$dao = Article_dao_factory::singleton()->get_dao();
			$article = $dao->find_by_number_article($this->number);
			$article->set_view($article->get_view()+1);
			$dao->update($article);
			
			$dao = Article_article_category_dao_factory::singleton()->get_dao();
			$category_number_list = $dao->find_by_number_article($article->get_number());
			$dao = Article_category_dao_factory::singleton()->get_dao();
			$basic_categories = NULL;
			$i = 0;
			foreach ($category_number_list as $category_number) {
				$category = $dao->find_by_number_article_category($category_number);
				$basic_categories[$i++] = new Basic_category_to($category->get_number(),$category->get_title());
			}

			$dao = Article_description_dao_factory::singleton()->get_dao();
			try {
				$description = $dao->find_last_by_article_number($article->get_number());
			}
			catch (Instance_not_found_exception $e) {
				$description = NULL;
			}
			
			$dao = Article_link_dao_factory::singleton()->get_dao();
			try {
				$links = $dao->find_by_article_number($article->get_number());
			}
			catch (Instance_not_found_exception $e) {
				$links = NULL;
			} 
			
			$dao = Article_image_dao_factory::singleton()->get_dao();
			try {
				$image = $dao->find_last_by_article_number($article->get_number());
			}
			catch (Instance_not_found_exception $e) {
				$image = NULL;
			} 
			
			$dao = Article_video_dao_factory::singleton()->get_dao();
			try {
				$video = $dao->find_last_by_article_number($article->get_number());
			}
			catch (Instance_not_found_exception $e) {
				$video = NULL;
			} 
			
			return new Basic_article_to($article->get_number(), $article->get_title(), $article->get_autor_login(), 
											$article->get_date(), $basic_categories, $description, $links, 
											$image, $video);
		}
		catch (Instance_not_found_exception $e) {
			throw new Model_exception($e);
		}
		catch (Internal_error_exception $e) {
			throw $e;
		}
		catch (Exception $e) {
			throw new Exception("Internal error exception --> " . $e);
		}
	}
	
}

?>
